var basePage = require('../../../common/BasePage');
var baseList = require('../../../common/BaseList');
Page({
    behaviors: [basePage, baseList],
    data: {
        reqUrl: '/home/user/collect/index?type=1',
        /**显示加入购物车弹窗 */
        addCart: false,
        /**选中的产品 */
        pdc: {},
        /**购买数量 */
        nums: 1,

    },
    onLoad: function (options) {
        // this.request({
        //     url: this.data.host + '/home/user/collect/index',
        //     data: { type: 1 } //0全部，1鲜花产品，2供应商
        // }, function(res) {
        //     console.log(res)
        // })
        this.req_items();
    },
    init_val(val, index, arr) {
        val.collect_info.collected=1;
        val.collect_info.thumb = this.get_thumb(val.collect_info.product_img);
    },
    /**
     *加入购物车事件
     */
    add_cart_click(e) {
        console.log(e);
        this.setData({
            addCart: true,
            pdc: e.detail,
        });
    },
    nums_change(e) {
        this.data.nums = e.detail;
        // console.log("nums_change",e)
    },
    /**
   * 添加产品到购物车
   */
  add_cart() {
    this.request({
      url: this.data.host + '/home/cart/addToCart',
      data: {
        pid: this.data.pdc.pid,
        nums: this.data.nums
      },
      method: 'post'
    }, this.add_cart_ok);
  },
  add_cart_ok(res) {
    wx.showToast({
      title: res.msg,
      icon: 'success'
    })
  },
    /**
   * 显示商家基地视频
   * @param {event} e 
   */
  show_video(e) {
    // console.log("show_video", e);
    // console.log(this.data.imgHost + '/' + e.detail);
    this.setData({
      showVideo: this.data.imgHost + '/' + e.detail
    })
  },
   /**
   * 收藏点击
   */
  collect_click(e) {
    console.log("collect_click", e);
    let pdc = e.detail;
    let index = e.currentTarget.dataset.index;
    let status = pdc.collected ? 0 : 1;
    this.request({
      url: this.data.host + '/home/user/collect/collect',
      data: {
        status: status,
        can_id: pdc.pid,
        type: 1
      },
      method: 'post'
    }, res => {
      console.log("collect_ok", res);
      let item = this.data.itemArr[index].collect_info;
      item.collected = status;
      item.collects = status ? (item.collects * 1) + 1 : item.collects - 1;
      this.setData({
        itemArr: this.data.itemArr
      })
    });
  },
  onShareAppMessage(res) {
    // console.log("onShareAppMessage",res);
    let pid=res.target.dataset.pid;
    let path="/pages/cls/index/index";
    let title="";
    if(pid){
      path='/pages/cls/pdcList/Index?pid='+pid;
      title="您的朋友向你推荐了一款鲜花"
    }
    return {
      title: title,
      path: path,
      // promise 
    }
  }
})